package msz.controller;

import com.github.pagehelper.PageInfo;
import msz.domain.Item;
import msz.domain.ItemCategory;
import msz.domain.ItemCategoryVo;
import msz.service.ItemCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RequestMapping("itemCategory")
public class ItemCategoryController {
    @Autowired
    ItemCategoryService itemCategoryService;

    @RequestMapping("/findAllByPage")
    public String findAll(Model model, @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        List<ItemCategory> itemCategoryList = itemCategoryService.findAll(currentPage);
        PageInfo<ItemCategory> pageInfo = new PageInfo<>(itemCategoryList);
        model.addAttribute("pageInfo", pageInfo);
        return "itemCategory";
    }

    @RequestMapping("/deleteBatch")
    public String deleteBatch(Integer[] ids) {
        itemCategoryService.deleteBatch(ids);
        return "redirect:/itemCategory/findAllByPage";
    }

    @RequestMapping("/save")
    public String add(Item item) {
        itemCategoryService.add(item);
        return "redirect:/itemCategory/findAllByPage";
    }
    @RequestMapping("/update")
    public String update(Item item){
        System.out.println(item);
        itemCategoryService.update(item);
        return "redirect:/itemCategory/findAllByPage";
    }
    @RequestMapping("/findItemById")
    public String findItemById(Integer id,Model model){
        Item item = itemCategoryService.findItemById(id);
        model.addAttribute("item",item);
        return "update";
    }
}
